package com.cmc.cloud.cmclink.doc.controller.admin.si;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.api.document.req.cargomanifest.TjBlnoCntrReportVO;
import com.cmc.cloud.cmclink.doc.api.document.req.cargomanifest.TjBlnoCntrReqVO;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.BlnoCntrConvert;
import com.cmc.cloud.cmclink.doc.entity.BlnoCntrDO;
import com.cmc.cloud.cmclink.doc.service.BlnoCntrService;
import com.cmc.cloud.cmclink.doc.vo.blnocntrvo.*;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostvo.BlnoCntrReportReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostvo.BlnoCntrReportVO;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;


/**
 * 提单箱型信息前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 提单箱型信息")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/blno-cntr")
public class BlnoCntrController {

    @Resource
    private BlnoCntrService blnoCntrService;

    @Operation(summary = "创建提单箱型信息")
    @OperateLog(type = OperateTypeEnum.CREATE)
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr:create')")
    public CommonResult<Void> createBlnoCntr(@Valid @RequestBody BlnoCntrCreateReqVO createReqVO) {
        blnoCntrService.createBlnoCntr(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新提单箱型信息")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr:update')")
    public CommonResult<Void> updateBlnoCntr(@Valid @RequestBody BlnoCntrUpdateReqVO updateReqVO) {
        blnoCntrService.updateBlnoCntr(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除提单箱型信息")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr:delete')")
    public CommonResult<Void> deleteBlnoCntr(@PathVariable("id") Long id) {
        blnoCntrService.deleteBlnoCntr(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得提单箱型信息")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr:query')")
    public CommonResult<BlnoCntrRespVO> getBlnoCntr(@PathVariable("id") Long id) {
        BlnoCntrDO blnoCntr = blnoCntrService.getBlnoCntr(id);
        return CommonResult.success(BlnoCntrConvert.INSTANCE.convert(blnoCntr));
    }

    @Operation(summary = "获得提单箱型信息列表")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/list")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr:query')")
    public CommonResult<List<BlnoCntrRespVO>> getBlnoCntrList(@RequestParam("ids") Collection<Long> ids) {
        List<BlnoCntrDO> list = blnoCntrService.getBlnoCntrList(ids);
        return CommonResult.success(BlnoCntrConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "获得提单箱型信息分页")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr:query')")
    public CommonResult<PageResult<BlnoCntrRespVO>> getBlnoCntrPage(@Valid BlnoCntrPageReqVO pageVO) {
        PageResult<BlnoCntrDO> pageResult = blnoCntrService.getBlnoCntrPage(pageVO);
        List<BlnoCntrRespVO> results = BlnoCntrConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "检查箱号是否拼箱")
    @OperateLog(type = OperateTypeEnum.OTHER)
    @PostMapping("/check-lcl")
    public CommonResult<BlnoCntrCheckLclRespVO> checkLcl(@Valid @RequestBody BlnoCntrCheckLclReqVO checkLclReqVO) {
        BlnoCntrCheckLclRespVO result = blnoCntrService.checkLcl(checkLclReqVO);
        return CommonResult.success(result);
    }

    @Operation(summary = "根据提单号获取提单箱信息")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/getCntrByBlNo")
    @Parameter(name = "blNo", description = "提单号", required = true)
    public CommonResult<List<BlnoCntrRespVO>> getCntrByBlNo(@RequestParam("blNo") String blNo) {
        return CommonResult.success(BeanUtil.copyToList(blnoCntrService.getCntrByBlNo(blNo), BlnoCntrRespVO.class));
    }

    @Operation(summary = "上海理货比对报表查询箱单信息")
    @OperateLog(type = OperateTypeEnum.GET)
    @PostMapping("/getBlnoCntrReport")
    public CommonResult<PageResult<BlnoCntrReportVO>> getBlnoCntrReport(@RequestBody BlnoCntrReportReqVO reqVo) {
        PageResult<BlnoCntrReportVO> blnoCntrReport = blnoCntrService.getBlnoCntrReport(reqVo);
        return CommonResult.success(blnoCntrReport);
    }

    @Operation(summary = "天津报表查询信息")
    @OperateLog(type = OperateTypeEnum.GET)
    @PostMapping("/getTjBlnoCntr")
    public CommonResult<TjBlnoCntrReportVO> getTjBlnoCntr(@RequestBody TjBlnoCntrReqVO reqVo) {
        return CommonResult.success(blnoCntrService.getTjBlnoCntr(reqVo));
    }
}
